--POUR CONTROLE


define erp='erp_mli2.';
define outils='outils_mli2.';

--CREATE TABLE se_sto_imputstk AS (SELECT * FROM &erp.sto_imputstk WHERE 1=2);

/*
select * from (
SELECT &outils.stragg(source) source,imputstk_cod, enreg_id, enreg_s, imputstk_lib, imputstk_lib2
FROM (
  SELECT 'uv' source ,g.* FROM se_sto_imputstk g
  UNION ALL
  SELECT 'se' source ,g.* FROM &erp.sto_imputstk g
  )
GROUP BY imputstk_cod, enreg_id, enreg_s, imputstk_lib, imputstk_lib2
HAVING Count(1)!=2) r
ORDER BY imputstk_cod*/


DELETE FROM se_sto_imputstk;

 INSERT INTO se_sto_imputstk
 (SELECT 'UV' || id imputstk_cod,
         0 enreg_id,
         'N' enreg_s,
         libelle imputstk_lib,
         NULL imputstk_lib2
    FROM uv_pimpsto) ;

 UPDATE se_sto_imputstk tf SET enreg_id=(select enreg_id FROM &erp.sto_imputstk f WHERE f.imputstk_cod=tf.imputstk_cod) ;
 UPDATE se_sto_imputstk tf SET enreg_id=&outils.enreg_id.NEXTVAL WHERE enreg_id IS NULL OR enreg_id =0;



 CALL uvse_synchroverserp('imputstk_cod','STO_IMPUTSTK','O') ;

 commit;